package com.bw.trade.config;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.http.HttpUtil;
import com.bw.trade.mapper.TbTransactionLogMapper;
import com.bw.trade.pojo.TbTransactionLog;
import com.bw.trade.service.TbCheckBillExceptionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;

import java.math.BigDecimal;
import java.util.List;

//定时任务
public class TimedTask {

    @Autowired
    private TbCheckBillExceptionService tbCheckBillExceptionService;
    @Autowired
    private TbTransactionLogMapper tbTransactionLogMapper;

    //定时对账
    @Scheduled(cron = "0 0 * * * ?")
    public void searchOrder() {
        System.out.println("定时器开启");
        //调用对账方法
        tbCheckBillExceptionService.abnormal();

    }

    //定时下载文件
    @Scheduled(cron = "0 0 23 * * ?")
    public void transaction(){

        HttpUtil.downloadFile("http://127.0.0.1:9091/api/common/downTradeLogFile", "E:\\JavaLianXi\\book\\loans\\credit-trade-web\\src\\main\\resources\\file");
        FileReader fileReader = new FileReader("E:\\JavaLianXi\\book\\loans\\credit-trade-web\\src\\main\\resources\\file\\2021-03-28-file.txt");
        List<String> strings = fileReader.readLines();
        strings.remove(0);
        for (String string : strings) {
            String[] split = string.split("\\|");
            TbTransactionLog tbTransactionLog = new TbTransactionLog();
            tbTransactionLog.setTransactionNo(split[0]);
            tbTransactionLog.setBillNo(split[1]);
            BigDecimal bigDecimal = new BigDecimal(split[2]);
            tbTransactionLog.setTransactionMoney(bigDecimal);
            tbTransactionLog.setTransactionStatusCode(split[3]);
            tbTransactionLog.setTransactionStatus(split[4] == "交易成功" ? 0 : split[4] == "交易失败" ? 1 : 2);
            DateTime parse = DateUtil.parse(split[5], "yyyy-MM-dd HH:mm:ss");
            tbTransactionLog.setTransactionTime(parse);
            tbTransactionLogMapper.insert(tbTransactionLog);

        }
    }
}
